#define _CRT_SECURE_NO_WARNINGS 1

#include "Stack.h"

void StackInit(ST* pst)
{
	assert(pst);

	pst->a = NULL;
	pst->capacity = pst->top = 0;
}


void StackDestroy(ST* pst)
{
	free(pst->a);
	pst->a = NULL;

	pst->top = 0;
	pst->capacity = 0;
}

void StackPush(ST* pst, STDataType x)
{
	assert(pst);

	if (pst->top == pst->capacity)
	{
		int newCapacity = (pst->capacity == 0 ? 4 : pst->capacity * 2);
		STDataType* tmp = (STDataType*)realloc(pst->a, sizeof(STDataType) * newCapacity);
		{
			if (tmp == NULL)
			{
				perror("realloc fail");
				exit(-1);
			}

			pst->a = tmp;
			pst->capacity = newCapacity;
		}
	}

	pst->a[pst->top] = x;
	pst->top++;

}

void StackPop(ST* pst)
{
	assert(pst);
	assert(pst->top > 0);

	pst->top--;
}

bool StackEmpty(ST* pst)
{
	assert(pst);

	return pst->top == 0;

}

int StackSize(ST* pst)
{
	assert(pst);

	return pst->top;
}

STDataType StackTop(ST* pst)
{
	assert(pst);

	return pst->a[pst->top - 1];
}

